/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package songmq;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class SongMQ {

    
    private Vector queue = null;

    public SongMQ() {
        queue = new Vector();
    }
    /**
     *  一个队列的简单实现， 向队列里面添加值
     * @param obj 
     */
    public synchronized void  send(Object obj)
    {
        queue.addElement(obj);
    }
    
    /**
     *  向队列里面取出新的值
     * @return 
     */
    public synchronized Object recv()
    {
        if(queue.size() ==0 )
            return null;
        Object obj = queue.firstElement();
        queue.removeElementAt(0);
        return obj;
    }
    
    
    private Integer offerMaxQueue = 200;
    
    private static List<Queue> queueCache = new LinkedList<Queue>();
    
    
    
    public  void startMQ()
    {
                new Thread(){

                    @Override
                    public void run() {
                        while(true)
                        {
                            synchronized(queueCache)
                            {
                                Integer size =  queueCache.size();
                                
                                if(size ==0)
                                {
                                    try {
                                        queueCache.wait();
                                    } catch (InterruptedException ex) {
                                        Logger.getLogger(SongMQ.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                }
                                
                                Queue queue = queueCache.remove(0);
                                
                            }
                        }
                    }        
                }.start();
    }
    
    
}
